package com.wenjiang.config;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class RabbitMqConfig {

    //定义交换机名称
    private final String ex_order="orderExChange";

    //定义发送邮件的交换机
    private final String  ex_email="emailExChange";





    //定义订单队列名称
    private  final String order_queue="orderQueue";
    //定义路由key
    private  final String order_key="orderKey";


    //定义发送邮件的名称
    private final String   register_code_queue="emailQueue";






    /**
     * 死信交换机 处理消费失败
     */

    private final  String ex_dead="deadExchange";

    /**
     * 订单超时队列
     */

    private final  String order_timeout_queue="orderTimeOutQueue";

    /**
     * 订单超时路由key
     */

    private final String order_timeout_key="orderTimeOut";



    //声明交换机 完全根据路由key 投递消息


    //订单业务交换机
    @Bean
    public DirectExchange orderExChange(){
        return  new DirectExchange(ex_order);
    }

    //死信交换机

    @Bean
    public DirectExchange  deadExchange(){
        return  new DirectExchange(ex_dead);
    }





    //声明队列


    //无消费者的延时队列
    @Bean
    public Queue orderQueue(){
        //绑定死信交换机
        Map<String,Object> arguments= new HashMap<>();

        arguments.put("x-dead-letter-exchange",ex_dead);
        arguments.put("x-dead-letter-routing-key",order_timeout_key);

        //60秒后如果没被消费 把消息投递给死信队列

        arguments.put("x-message-ttl", 60000*10);


        return new Queue(order_queue,true,false,false,arguments);
    }

    //验证码发送队列
    @Bean
    public Queue registerCodeQueue(){
        return new Queue(register_code_queue,true,false,false);
    }

    //订单超时队列
    @Bean
    public Queue orderTimeOutQueue(){
        return new Queue(order_timeout_queue);
    }

    /**
     * 订单交换机绑定 队列
     */
    @Bean
    public Binding orderBinding(){
        return BindingBuilder.bind(orderQueue()).to(orderExChange()).with(order_key);
    }

    /**
     * 死信交换机绑定队列
     */
    @Bean
    public  Binding deadBinding(){
        return BindingBuilder.bind(orderTimeOutQueue()).to(deadExchange()).with(order_timeout_key);
    }








}
